import request from './index'

interface LoginParams {
  username: string
  password: string
  captcha_id: string
  captcha_code: string
}

interface NewsListParams {
  current: number
  currentSize: number
}

interface NewsInser {
  id?: string
  news_title: string
  title?: string
  abstractReal: string
  classReal?: number
  classModel?: string
  newsSource?: string
  newsTime?: string
  coverImage?: string
  isSelect?: number
  addMode: number
  siteName?: string
  opPerson?: string
  createTime?: string
  pubTime?: null
  isPublished?: number
  markContent?: string
  sourceUrl?: string
}

interface PublishSettings {
  waitPubTime?: string
  issueTitle: string
  issueCoverImage: string
  issueDescription: string
  issuePublisher: string
}
export const loginApi = (data: LoginParams) => {
  return request.post('/auth/login', data)
}

export const getcaptcha = () => {
  return request.get('/auth/captcha')
}

export const getNewsList = (params: NewsListParams) => {
  return request.get('/news/newsList', { params })
}

export const publishNews = () => {
  return request.post('/news/releaseList')
}

export const updateNews = (data: { id: number; [key: string]: any }) => {
  const { access_token } = JSON.parse(localStorage.getItem('login')!)
  return request.post('/news/updateNews', data, {
    headers: {
      Authorization: `Bearer ${access_token}`,
    },
  })
}

export const insertNews = (data: Omit<NewsInser, 'id'>) => {
  return request.post('/news/insertNews', data)
}

export const sortord = (list: { id: number; orderNews: number }[]) => {
  return request.post('/news/order', { list })
}
export const settings = (data: PublishSettings) => {
  const { access_token } = JSON.parse(localStorage.getItem('login')!)
  const requestData = {
    ...data,
    attributes: {
      roleCodes: ['admin'],
    },
  }
  return request.post('/news/publishReleaseList', requestData, {
    headers: {
      Authorization: `Bearer ${access_token}`,
    },
  })
}

export const getIssue = (data: NewsListParams) => {
  return request.post('/news/issueList', data)
}



export const deleteNews = (id: number) => {
  return request.post('/news/deleteNews', { id })
}


export const getINews = ( issueNo: number) => {
  return request.get(`/news/issueNews?issueNo=${issueNo}`)
}